<?php

/**
 * Implementation of hook_form().
 */
function qdrupal_link_form(&$node) {
  qdrupal_bootstrap();
  $type = node_get_types('type', $node);
  
  if ($type->has_title) {
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => check_plain($type->title_label),
      '#default_value' => $node->title,
      '#required' => TRUE,
      '#weight' => -5,
      '#description' => t('The descriptive name of your QForm.'),
    );
  }

  $formPath = QDRUPAL_FORMS;
  file_check_directory($formPath, FILE_CREATE_DIRECTORY);

  function qdrupal_walkFormDir($strPath) {
	$fileList = array();
    foreach(scandir($strPath) as $file) {
	  if($file != '.' && $file != '..') {
		$filePath = $strPath.'/'.$file;
		if(is_dir($filePath)) 
		  $fileList = array_merge(qdrupal_walkFormDir($filePath),$fileList);
		else
		  $fileList[$filePath] = str_replace(QDRUPAL_FORMS,'',$filePath);
	  }
	}
	return $fileList;
  }
  $fileList = qdrupal_walkFormDir(QDRUPAL_FORMS);
  ksort($fileList);

  $form['form_path'] = array(
    '#type' => 'select',
    '#title' => t('Select from the existing list of files'),
    '#default_value' => '',
    '#options' => $fileList,
    '#weight' => -5,
    '#description' => t('Choose a file to link this node with.  Files must be placed within '.QDRUPAL_FORMS.'.')
  );

  return $form;
}

/**
 * Implementation of hook_insert().
 */
function qdrupal_link_insert($node) {
  db_query("INSERT INTO {qdrupal_link} (nid, vid, form_path) VALUES (%d, %d, '%s')", 
    $node->nid, $node->vid, $node->form_path);
}

/**
 * Implementation of hook_load().
 */
function qdrupal_link_load($node) {
  $qdrupal_link = db_fetch_object(db_query('SELECT form_path FROM {qdrupal_link} WHERE vid = %d AND nid = %d', $node->vid, $node->nid));
  return $qdrupal_link;
}

/**
 * Implementation of hook_view().
 */
function qdrupal_link_view($node, $teaser = FALSE, $page = FALSE) {
  if($page) {
    /**
     * This is where Qcodo gets instantiated and executed
     */
    //$strContent = 'Execute: '.$node->form_path;
    qdrupal_prepend();

	if(variable_get('qdrupal_enable_profiling',false)) {
	  foreach(QApplication::$Database as $objDb) 
		$objDb->EnableProfiling();
	}

    try {
	  if(file_exists($node->form_path)) {
		ob_start();
		require($node->form_path);
		$strContent = ob_get_clean();
	  }
    }
    catch (QDrupalException $e) {
	  $strContent = $e->getMessage();
    }
    catch (Exception $e) {
      ob_start();
      QcodoHandleException($e,FALSE);
      $strContent = ob_get_clean();
    }

	if(variable_get('qdrupal_enable_profiling',false)) {
	  ob_start();
	  foreach(QApplication::$Database as $objDb) 
		$objDb->OutputProfiling();
	  $strContent .= ob_get_clean();
	}
  }
  else {
	$strContent = "QForms are not displayed in the node's teaser view, please ";
  }

  $node = node_prepare($node, $teaser);
  $node->content['info_content'] = array(
    '#value' => $strContent,
    '#weight' => 1,
  );
  return $node;
}

/**
 * Implementation of hook_update().
 */
function qdrupal_link_update($node) {
  // if this is a new node or we're adding a new revision,
  if ($node->revision) {
    qdrupal_link_insert($node);
  }
  else {
    db_query("UPDATE {qdrupal_link} SET form_path = '%s' WHERE vid = %d AND nid = %d", $node->form_path,$node->vid,$node->nid);
  }
}

/**
 * Implementation of hook_delete().
 */
function qdrupal_link_delete($node) {
  db_query('DELETE FROM {qdrupal_link} WHERE nid = %d', $node->nid);		
}

/**
 * Implementation of hook_nodeapi().
 */
function qdrupal_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'delete revision':
      db_query('DELETE FROM {qdrupal_link} WHERE vid = %d', $node->vid);
      break;
  }
}
